/**
 * 作者: wgx
 * 日期: 2025-03-18 17:50:54
 * 备注: 用户幸存者派对模块数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserFeastInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserFeastInfoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected int score; // 积分人气值
    protected long resetTime; // 每日重置次数
    protected int joinNum; // 使用金币参加普通派对的次数 (每日重置，不能超过3次)

    public void reset() {
    }

    protected String getKey() {
        return "UserFeastInfo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_feast_info"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("score", score); // 积分人气值
        obj.put("resetTime", resetTime); // 每日重置次数
        obj.put("joinNum", joinNum); // 使用金币参加普通派对的次数 (每日重置，不能超过3次)
        return obj;
    }

    public PBUserFeastInfo.Builder takeInitBuilder() {
        PBUserFeastInfo.Builder sb = PBUserFeastInfo.newBuilder();
        if (score != 0) {
            sb.setScore(score);
        }
        if (joinNum != 0) {
            sb.setJoinNum(joinNum);
        }
        return sb;
    }

    public PBUserFeastInfo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserFeastInfo.Builder sb = PBUserFeastInfo.newBuilder();
        if (isFieldUpdate("score")) {
            sb.setScore(score);
        }
        if (isFieldUpdate("joinNum")) {
            sb.setJoinNum(joinNum);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getScore() {
        return score;
    }

    public void putScore(int score) {
        if (this.score != score) {
            this.score = score;
            updateRedisHashField(getKey(), "score", this.score);
        }
    }

    public void addScore(int value) {
        this.score += value;
        updateRedisHashField(getKey(), "score", this.score);
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public int getJoinNum() {
        return joinNum;
    }

    public void putJoinNum(int joinNum) {
        if (this.joinNum != joinNum) {
            this.joinNum = joinNum;
            updateRedisHashField(getKey(), "joinNum", this.joinNum);
        }
    }

    public void addJoinNum(int value) {
        this.joinNum += value;
        updateRedisHashField(getKey(), "joinNum", this.joinNum);
    }

}